package com.tianze.tzkf.function.home;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tianze.tzkf.R;

/**
 * Created by Zcyang on 2018/12/27.
 */
public class HomeMenuAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private int selectionPosition = -1;

    /**
     * 用于填充ListView的数据，这里就简单只用了两条数据。
     */
    private String[] menuItems = {"交易记录", "系统配置", "硬件信息", "关于我们"};


    public HomeMenuAdapter(Context context) {
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return menuItems.length;
    }

    @Override
    public Object getItem(int position) {
        return menuItems[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_home_menu , null);
            viewHolder.tvMenu = (TextView) convertView.findViewById(R.id.tv_menu);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tvMenu.setText(menuItems[position]);
        if (selectionPosition == position)
        {
            viewHolder.tvMenu.setBackgroundResource(R.drawable.home_menu_select);
        }
        else
        {
            viewHolder.tvMenu.setBackgroundColor(Color.TRANSPARENT);
        }
        return convertView;

    }

    public void setSelectedPosition(int position)
    {
        this.selectionPosition = position;
    }


    class ViewHolder
    {
        TextView tvMenu,tvSelect;
    }

}
